package com.bckj.fastboot.upms.biz.service;

import com.bckj.fastboot.upms.api.dto.UserAuthInfo;
import com.bckj.fastboot.upms.biz.repository.UserRepository;
import com.bckj.fastboot.upms.biz.convert.UserConvert;
import com.bckj.fastboot.upms.api.vo.UserVO;
import com.bckj.fastboot.upms.api.dto.UserRequest;
import com.bckj.fastboot.upms.api.dto.UserQuery;
import com.bckj.fastboot.upms.api.entity.User;

import com.bckj.fastboot.core.lang.exception.BusinessException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户表(User)domain-service
 *
 * @author wfc
 * @since 2023-09-14 16:05:36
 */
@Service
@RequiredArgsConstructor
public class UserService {

    private final UserRepository userRepository;
	
    private final UserConvert userConvert;
	
    /**
     * 列表
     *
     * @param query req
     * @return list
     */
    public List<UserVO> list(UserQuery query){
        return userRepository.getBaseMapper().list(query);
    }
    
     /**
     * 保存
     *
     * @param request req
     */
    public boolean save(UserRequest request) {
        return userRepository.save(userConvert.request2Bean(request));
    }

    /**
     * 修改
     *
     * @param request req
     */
    public boolean update(UserRequest request) {
        return userRepository.updateById(userConvert.request2Bean(request));
    }
    
    /**
     * 详情
     *
     * @param id id
     * @return vo
     */
    public UserVO details(Long id) {
        User bean = userRepository.getById(id);
        if (null == bean) {
            throw new BusinessException("当前对象不存在!");
        }
        return userConvert.bean2VO(bean);
    }

	/**
	 * 根据Id移除
	 *
	 * @param id id
	 * @return boolean
	 */
	public boolean remove(Long id) {
        //TODO LIMIT
		return userRepository.removeById(id);
	}

    public UserAuthInfo findByPhone(String phone) {
        return userRepository.getBaseMapper().findByPhone(phone);
    }
}

